package com.itheima.tliasmanager.controller;

import com.itheima.tliasmanager.annotation.Log;
import com.itheima.tliasmanager.pojo.domain.Emp;
import com.itheima.tliasmanager.pojo.Vo.PageResult;
import com.itheima.tliasmanager.pojo.Dto.QueryParam;
import com.itheima.tliasmanager.pojo.Vo.Result;
import com.itheima.tliasmanager.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(QueryParam queryParam){

        log.info("分页查询员工列表，参数：{}",queryParam);

        PageResult<Emp> pageResult = empService.page(queryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询所有员工");
        List<Emp> list = empService.list();
        return Result.success(list);
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("添加员工，参数：{}",emp);
        empService.saveEmp(emp);
        return Result.success();
    }

    @Log
    @DeleteMapping
    public Result batchRemove(/*Integer[] ids*/ @RequestParam List<Integer> ids){
        log.info("批量删除员工，参数：{}", ids);
        empService.batchRemove(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询员工，id：{}",id);
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工，参数：{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }

}
